/**
 * 
 */
package com.huarui.desktop;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.PagingInfo;
import com.huarui.desktop.model.Desktop;
import com.huarui.desktop.model.DesktopMenu;
import com.huarui.desktop.service.DesktopInfoService;
import com.opensymphony.xwork2.Action;

/**
 * @author sunpeng
 * 
 */
public class DesktopInfoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7281125787648693484L;
	private DesktopInfoService desktopInfoService;

	public String list() throws Exception {
		PagingInfo<Desktop, Desktop> paginInfo = new PagingInfo<Desktop, Desktop>();
		int start = super.getIntegerParameter("start", 0);
		int limit = super.getIntegerParameter("limit", 20);
		paginInfo.setStart(start);
		paginInfo.setLimit(limit);
		this.desktopInfoService.queryDesktopInfos(paginInfo);
		ServletActionContext.getContext().put("jsonResult", paginInfo);
		return Action.SUCCESS;
	}

	public String add() {
		Desktop desktop=this.getDesktop();
		this.desktopInfoService.addDesktop(desktop);
		ServletActionContext.getContext().put("jsonResult", desktop);
		return Action.SUCCESS;
	}

	public String modify() {
		Desktop desktop=this.getDesktop();
		this.desktopInfoService.modifyDesktop(desktop);
		ServletActionContext.getContext().put("jsonResult", desktop);
		return Action.SUCCESS;
	}
	
	public String listSubMenus(){
		int desktopId = super.getIntegerParameter("desktopId", 0);
		int menuId = super.getIntegerParameter("menuId", 0);
		List<DesktopMenu> menus=this.desktopInfoService.querySubMenus(desktopId, menuId);
		ServletActionContext.getContext().put("jsonResult", menus);
		return Action.SUCCESS;
	}
	
	private Desktop getDesktop() {
		Integer id = super.getIntegerParameter("id", 0);
		String name = super.getSingleParameter("name");
		String description = super.getSingleParameter("description");
	
		Desktop desktop = new Desktop();
		desktop.setId(id);
		desktop.setName(name);
		desktop.setDescription(description);
		
		return desktop;
	}

	public void setDesktopInfoService(DesktopInfoService desktopInfoService) {
		this.desktopInfoService = desktopInfoService;
	}

}
